package com.meida.boot.handler.app;

import com.meida.common.enums.OnLineEnum;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.product.client.entity.ProdProduct;
import com.meida.module.product.client.entity.ProdShop;
import com.meida.module.product.provider.service.ProdShopService;
import com.meida.common.enums.ColleconEnum;
import com.meida.module.system.provider.service.SysColleconService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 商家店铺产品列表
 *
 * @author Administrator
 */
@Component("shopProductListHandler")
@Log4j2
public class ShopProductListHandler implements PageInterceptor<ProdShop> {
    @Autowired
    private SysColleconService colleconService;

    @Autowired
    private ProdShopService shopService;


    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        String shopId = params.get("shopId");
        cq.eq(ProdProduct.class, "shopId", shopId);
        cq.eq(ProdProduct.class, "onLineState", OnLineEnum.ON.getCode());
    }

    @Override
    public void complete(CriteriaQuery<ProdShop> cq, List<EntityMap> result, EntityMap extra) {
        Long shopId = cq.getRequestMap().getLong("shopId");
        if (FlymeUtils.isNotEmpty(shopId)) {
            ProdShop shop = shopService.getById(shopId);
            if (FlymeUtils.isNotEmpty(shop)) {
                extra.put("shopLogo", shop.getShopLogo());
                extra.put("shopId", shop.getShopId());
                extra.put("shopName", shop.getShopName());
                //店铺收藏标记
                Integer shopScTag = colleconService.countCollecon(shopId, ColleconEnum.SC, ProdShop.class);
                extra.put("shopScTag", shopScTag);
                //店铺收藏总数
                Integer shopScCount = colleconService.countAllCollecon(shopId, ColleconEnum.SC, ProdShop.class);
                extra.put("shopScCount", shopScCount);
                //店铺评分
                extra.put("shopScore", shop.getShopScore());
            }
        }

    }
}
